package com.jhclothing.product.service.impl;

import com.jhclothing.common.constant.UserConstants;
import com.jhclothing.common.utils.StringUtils;
import com.jhclothing.product.domain.Fabric;
import com.jhclothing.product.mapper.FabricMapper;
import com.jhclothing.product.service.IFabricService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 布料Service业务层处理
 *
 * @author zyp
 * @date 2024-04-20
 */
@Service
public class FabricServiceImpl implements IFabricService {
    @Autowired
    private FabricMapper fabricMapper;

    /**
     * 查询布料
     *
     * @param fabricId 布料主键
     * @return 布料
     */
    @Override
    public Fabric selectFabricByFabricId(Long fabricId) {
        Fabric fabric = fabricMapper.selectFabricByFabricId(fabricId);
        if (StringUtils.isNotEmpty(fabric.getFabricName())) {
            fabric.setFabricCodeAndName(fabric.getFabricCode() + " - " + fabric.getFabricName());
        } else {
            fabric.setFabricCodeAndName(fabric.getFabricCode());
        }
        return fabric;
    }

    /**
     * 查询布料列表
     *
     * @param fabric 布料
     * @return 布料
     */
    @Override
    public List<Fabric> selectFabricList(Fabric fabric) {
        return fabricMapper.selectFabricList(fabric);
    }

    /**
     * 查询所有布料
     *
     * @return 布料列表
     */
    @Override
    public List<Fabric> selectFabricAll() {
        List<Fabric> fabrics = fabricMapper.selectFabricAll();
        fabrics.stream()
                .map(fabric -> {
                    if (StringUtils.isNotEmpty(fabric.getFabricName())) {
                        fabric.setFabricCodeAndName(fabric.getFabricCode() + " - " + fabric.getFabricName());
                    } else {
                        fabric.setFabricCodeAndName(fabric.getFabricCode());
                    }
                    return fabric;
                })
                .collect(Collectors.toList());
        return fabrics;
    }

    /**
     * 新增布料
     *
     * @param fabric 布料
     * @return 结果
     */
    @Override
    public int insertFabric(Fabric fabric) {
        return fabricMapper.insertFabric(fabric);
    }

    /**
     * 修改布料
     *
     * @param fabric 布料
     * @return 结果
     */
    @Override
    public int updateFabric(Fabric fabric) {
        return fabricMapper.updateFabric(fabric);
    }

    /**
     * 修改布料库存
     *
     * @param fabricId     布料ID
     * @param nowInventory 布料库存
     */
    @Override
    public int updateFabricInventory(Long fabricId, BigDecimal nowInventory) {
        return fabricMapper.updateFabricInventory(fabricId, nowInventory);
    }

    /**
     * 批量删除布料
     *
     * @param fabricIds 需要删除的布料主键
     * @return 结果
     */
    @Override
    public int deleteFabricByFabricIds(Long[] fabricIds) {
        return fabricMapper.deleteFabricByFabricIds(fabricIds);
    }

    /**
     * 校验布料编码是否唯一
     *
     * @param fabric 布料信息
     * @return 结果
     */
    @Override
    public boolean checkFabricCodeUnique(Fabric fabric) {
        Long fabricId = (null == fabric.getFabricId()) ? -1L : fabric.getFabricId();
        Fabric info = fabricMapper.checkFabricCodeUnique(fabric.getFabricCode());
        if ((info != null) && info.getFabricId().longValue() != fabricId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
